package com.navercorp.pinpoint.web.dao.es;

import com.navercorp.pinpoint.common.hbase.HBaseTables;
import com.navercorp.pinpoint.common.server.bo.AgentInfoBo;
import com.navercorp.pinpoint.web.dao.es.entity.AgentInfoEntity;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Index;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.IOException;


@Repository
public class EsAgentInfoDao {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JestClient jestClient;

    /**
     * 插入agent基础数据
     * @param agentInfo
     */
    public void insert(AgentInfoEntity agentInfo) {
        if (agentInfo == null) {
            throw new NullPointerException("agentInfo must not be null");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("insert agent info. {} to ES", agentInfo);
        }
        Index.Builder builder = new Index.Builder(agentInfo);
        builder.id(agentInfo.getAgentId());
        builder.refresh(true);
        Index default_space = builder.index("default_space").type(HBaseTables.AGENTINFO_STR).build();
        JestResult result;
        try {
            result = jestClient.execute(default_space);
            if (result != null && result.isSucceeded()) {
                logger.info("ESJestClient insert AgentInfo success");
            }
        } catch (IOException e) {
            logger.error("ESJestClient insertDoc AgentInfo exception", e);
        }
    }
}
